package com.shgsh.platform.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by jessy on 2015/8/7.
 */
@Controller
@RequestMapping("/plantform")
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);



    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login()
    {
        Subject subject= SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return "redirect:/plantform/home";
        }
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(Model model, HttpServletRequest request)
    {
        Subject subject=SecurityUtils.getSubject();
        if(subject.isAuthenticated()||subject.isRemembered()){
            return "redirect:/plantform/home";
        }

        String username = request.getParameter("username");
        String password = request.getParameter("password");
//        String loginKaptchaCode = request.getParameter("code");

        Session shiroSession = subject.getSession();
        /*验证码验证
         Object kaptchaCode = shiroSession.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

       if (kaptchaCode!=null && !StringUtils.equalsIgnoreCase(loginKaptchaCode, kaptchaCode.toString()))
        {
            model.addAttribute("message", "验证码错误!");
            return "/login";
        }*/

        UsernamePasswordToken token = new UsernamePasswordToken(username, password, false, request.getRemoteHost());
        try
        {
            subject.login(token);
            /*User user = jpaRealmRepository.findUserByName(username);
            user.setLastLogin(new Date());
            user = jpaRealmRepository.mergeUser(user);*/

            return "redirect:/plantform/home";
        } catch (UnknownAccountException uae)
        {
            model.addAttribute("message", "Unknown User!");
            log.info("Unknown User!");
        } catch (IncorrectCredentialsException ice)
        {
            model.addAttribute("message", "Incorrect Password!");
            log.info("Incorrect Password!");
        } catch (LockedAccountException lae)
        {
            model.addAttribute("message", "User Locked!");
            log.info("User Locked!");
        } catch (AuthenticationException ae)
        {
            model.addAttribute("message", "Authentication Failed!");
            log.info("Authentication Failed!");
        }
        return "/login";
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(Locale locale, Model model)
    {

        log.info("Welcome home! the client locale is " + locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);
        model.addAttribute("serverTime", formattedDate);

        return "/home/home";
    }
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout() {
        SecurityUtils.getSubject().logout();
        return "redirect:/plantform/home";
    }


}


